<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of CreateBookmarkCommand
 *
 * @author Manu
 */
require_once("mmw_command_Command.php");
require_once(dirname(__FILE__) . "/../core/mmw_core_AppException.php");
require_once(dirname(__FILE__) . "/../controller/mmw_controller_Request.php");
require_once(dirname(__FILE__) . "/../domain/mmw_domain_Webpage.php");
require_once(dirname(__FILE__) . "/../domain/mmw_domain_Tag.php");
require_once(dirname(__FILE__) . "/../domain/mmw_domain_Bookmark.php");
require_once(dirname(__FILE__) . "/../mapper/mmw_mapper_WebpageMapper.php");
require_once(dirname(__FILE__) . "/../mapper/mmw_mapper_TagMapper.php");
require_once(dirname(__FILE__) . "/../mapper/mmw_mapper_BookmarkMapper.php");

class mmw_command_CreateBookmarkCommand extends mmw_command_Command {

    public function doExecute(mmw_controller_Request $request) {
        $success = true;
        $missingParameter;

        if ($request->getProperty("url") == null) {
            $success = false;
            $missingParameter = "url";
        } else if ($request->getProperty("title") == null) {
            $success = false;
            $missingParameter = "title";
        } else if ($request->getProperty("tags") == null) {
            $success = false;
            $missingParameter = "tags";
        }

        if (!$success) {
            throw new mmw_core_AppException("Missing parameter '$missingParameter'");
        }

        $url = $request->getProperty("url");
        $title = $request->getProperty("title");
        $strTags = $request->getProperty("tags");

        //1) we check if the webpage exists. if not, we create it
        //does the webpage exists ?
        $webpageMapper = new mmw_mapper_WebpageMapper();
        $webpage = $webpageMapper->findByUrl($url);

        if ($webpage == null) {
        //doesn't exist. we create it.
            $webpage=new mmw_domain_Webpage();
            $webpage->setUrl($url);
            $webpage->setTitle($title);
            
            $webpageMapper->insert($webpage);
            
            $request->addFeedback("Created new webpage with id=".$webpage->getId());
        }

        //2) we check if the tags exist. If not, we create them
        $arOTags=array(); //the array of Tag objects
        $arStrTags=explode(",",$strTags);
        
        for ($i=0;$i<count($arStrTags);$i++) {
            $strCurrentTag=$arStrTags[$i];
            
            //does the tag exists?
            $tagMapper=new mmw_mapper_TagMapper();
            $oTag=$tagMapper->findByTag($strCurrentTag);
            
            if ($oTag==null) {
            //it doesn't exist. we create it
                $oTag=new mmw_domain_Tag();
                $oTag->setTag($strCurrentTag);
                
                $tagMapper->insert($oTag);
                
                $request->addFeedback("Created new tag with id=".$oTag->getId());
            }
            
            $arOTags[]=$oTag;
            
        }
        
        //3) For each tag object in $arOTags, we check if the association webpage_id, tags_id exists. If not we create it
        $bookmarkMapper=new mmw_mapper_BookmarkMapper();
        
        for ($i=0;$i<count($arOTags);$i++) {
            //does the association exist?
            $doesExist=$bookmarkMapper->existsHasIds($webpage->getId(), $arOTags[$i]->getId());
            
            if (!$doesExist) {
            //doesn't exist: we create it
                $bookmarkMapper->insertIds($webpage->getId(), $arOTags[$i]->getId());
                
                $request->addFeedback("Created new association (webpage_id,tags_id) : (".$webpage->getId().",".$arOTags[$i]->getId().")");
            }
        }
        
        $request->setSuccess(true);
    }

}

//********Tests
//$createBookmarkCmd = new mmw_command_CreateBookmarkCommand();
//$request = new mmw_controller_Request();
//$request->setProperty("url", "whatever6.com");
//$request->setProperty("title","whatever5");
//$request->setProperty("tags","test44,test45");
//$createBookmarkCmd->doExecute($request);
//
//var_dump($request->getFeedback());
?>
